<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\ChunkRepository;
use App\Http\Servers\Admin\ChunkServer;
use App\Http\Servers\Admin\InfoServer;
use App\Http\Servers\Admin\SortServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ChunkController extends CommonController
{
    //信息模块列表
    public function chunk(Request $request, ChunkServer $chunkServer, ChunkRepository $chunkRepository)
    {
        if ($request->has('del')) {

            if ($message = $chunkServer->sofeVerify($request->del)) {
                $message['message'] = $message;
                return back()->with($message);
            }

            $chunk = \App\Chunk::findorfail($request->del);
            $result = $chunk->delete();
            if ($result) {
                \App\ChunkInfoFieldLines::where('chunk', $request->del)->delete();

                $message['message'] = message(true, '删除成功', '删除信息模块 ' . $chunk->name . ' 成功!');
            } else {
                $message['message'] = message(false, '删除失败', '删除信息模块 ' . $chunk->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['chunks'] = $chunkRepository->getChunkInfoWithTheme();

        return view('admin.chunk.chunk', $data);
    }

    //信息模块添加
    public function chunk_add(Request $request, ChunkServer $chunkServer, SortServer $sortServer, InfoServer $infoServer)
    {
        if ($request->isMethod('post')) {
            if ($nopower = $chunkServer->needRoot()) {
                $message['message'] = $nopower;
                return back()->with($message);
            }

            $this->validate($request, [
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:chunks',
                'paginate' => 'required|digits_between:1,2',
                'link_info_num' => 'required|digits:1',
                'theme' => 'required|chunk_theme',
                'sort_theme' => 'required|sort_theme',
                'info_theme' => 'required|info_theme',
            ]);

            $create['name'] = $request->name;
            $create['path'] = $request->path;
            $create['paginate'] = $request->paginate;
            $create['link_info_num'] = $request->link_info_num;
            $create['theme'] = $request->theme;
            $create['sort_theme'] = $request->sort_theme;
            $create['info_theme'] = $request->info_theme;
            $create['user_write'] = $request->has('user_write') ? 1 : 0;
            $create['status'] = $request->has('status') ? 1 : 0;
            $create['long_tail'] = $request->has('long_tail') ? 1 : 0;
            $create['describe'] = $request->describe;
            $create['extend'] = $request->extend;
            $create['simpledescribe'] = $request->has('simpledescribe') ?
                $request->simpledescribe
                : subtext(strip_tags($request->describe), config('app.simple_len'));
            $result = \App\Chunk::create($create);
            if ($result) {
                $chunkServer->setInfoFields($result->id, $request->infofield);

                $message['message'] = message(true, '添加成功', '添加信息模块 ' . $request->name . ' 成功!');
            } else {
                $message['message'] = message(false, '添加失败', '添加信息模块 ' . $request->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['chunkThemes'] = $chunkServer->getChunkTheme();
        $data['sortThemes'] = $sortServer->getSortTheme();
        $data['infoThemes'] = $infoServer->getInfoTheme();

        $data['infoFields'] = \App\InfoField::lists('name', 'id');

        return view('admin.chunk.chunk_add', $data);
    }

    //信息模块编辑
    public function chunk_edit(Request $request, ChunkServer $chunkServer, SortServer $sortServer, InfoServer $infoServer)
    {
        if ($request->isMethod('post')) {
            if ($nopower = $chunkServer->needRoot()) {
                $message['message'] = $nopower;
                return back()->with($message);
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'path' => 'required|alpha_dash|unique:chunks,path,' . $request->id . ',id',
                'paginate' => 'required|digits_between:1,2',
                'link_info_num' => 'required|digits:1',
                'theme' => 'required|chunk_theme',
                'sort_theme' => 'required|sort_theme',
                'info_theme' => 'required|info_theme',
            ]);

            $update['name'] = $request->name;
            $update['path'] = $request->path;
            $update['paginate'] = $request->paginate;
            $update['link_info_num'] = $request->link_info_num;
            $update['theme'] = $request->theme;
            $update['sort_theme'] = $request->sort_theme;
            $update['info_theme'] = $request->info_theme;
            $update['user_write'] = $request->has('user_write') ? 1 : 0;
            $update['long_tail'] = $request->has('long_tail') ? 1 : 0;
            $update['describe'] = $request->describe;
            $update['extend'] = $request->extend;
            $update['simpledescribe'] = $request->has('simpledescribe') ?
                $request->simpledescribe
                : subtext(strip_tags($request->describe), config('app.simple_len'));
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\Chunk::where('id', $request->id)->update($update);
            if ($result) {
                $chunkServer->setInfoFields($request->id, $request->infofield);

                $message['message'] = message(true, '编辑成功', '编辑信息模块 ' . $request->name . ' 成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '编辑信息模块 ' . $request->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['chunk'] = \App\Chunk::findorfail($request->id);
        $data['chunk']['info_fields'] = \App\ChunkInfoFieldLines::where('chunk', $request->id)
            ->lists('info_field')
            ->toArray();

        $data['chunkThemes'] = $chunkServer->getChunkTheme();
        $data['sortThemes'] = $sortServer->getSortTheme();
        $data['infoThemes'] = $infoServer->getInfoTheme();

        $data['infoFields'] = \App\InfoField::lists('name', 'id');

        return view('admin.chunk.chunk_edit', $data);
    }
}
